﻿//Copyright (c) 2010 Christopher E. S. King (http://chriskingconsulting.com)
//
//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files 
// (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, 
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do 
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;

namespace King.Extensions.Collections {

    public sealed class SynchronizedQueue<T> where T : class {

        private int? m_maxQueueSize;
        private ManualResetEvent m_itemAvailable;
        private ManualResetEvent m_spaceAvailable;
        private Queue<T> m_queue;

        public SynchronizedQueue(IEnumerable<T> collection = null, int? maxQueueSize = null) {
            Contract.Requires(maxQueueSize == null || maxQueueSize > 0);

            m_maxQueueSize = maxQueueSize;
            m_itemAvailable = new ManualResetEvent(false);
            m_spaceAvailable = new ManualResetEvent(true);
            m_queue = new Queue<T>(collection);
        }

        public int Count {
            get { lock (this) { return m_queue.Count; } }
        }
        public T Dequeue() {
            T result;

            while (!TryDequeue(out result))
                m_itemAvailable.WaitOne();

            return result;
        }
        public bool TryDequeue(out T result) {
            result = null;
            var success = false;

            lock (this) {

                if (m_queue.Count > 0) {
                    result = m_queue.Dequeue();
                    success = true;

                    if (m_queue.Count == 0)
                        m_itemAvailable.Reset();
                }
            }

            return success;
        }
        public void Enqueue(T item) {
            while (!TryEnqueue(item))
                m_spaceAvailable.WaitOne();
        }
        public bool TryEnqueue(T item) {
            var success = false;

            lock (this) {

                if (m_maxQueueSize != null && m_queue.Count < m_maxQueueSize) {
                    m_queue.Enqueue(item);
                    success = true;

                    if (m_maxQueueSize != null && m_queue.Count == m_maxQueueSize)
                        m_spaceAvailable.Reset();
                }
            }

            return success;
        }
        public T Peek() {
            lock (this) { return m_queue.Peek(); }
        }

        public WaitHandle ItemAvailable {
            get { return m_itemAvailable; }
        }
        public WaitHandle SpaceAvailable {
            get { return m_spaceAvailable; }
        }
    }
}